from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.sms.v20210111 import sms_client, models
from django.conf import settings

def tencent_send_mes(phone,code,TemplateId = "449739"):
    '''这里面是tencentcloud-sdk-python的简化示例代码进行了参数修改'''
    try:
        phone = "{}{}".format('+86', phone)  # 手机号码， 其中前面有一个+号 ，86为国家码
        cred = credential.Credential(settings.secretId,settings.secretKey)# 实例化一个认证对象
        client = sms_client.SmsClient(cred, settings.city)  # 实例化要请求产品(以sms为例)的client对象
        req = models.SendSmsRequest() # 实例化一个请求对象

        req.SmsSdkAppId = settings.SmsSdkAppId# 短信应用ID
        req.SignName = settings.SignName#签名
        req.TemplateId = TemplateId # 模板 ID
        req.TemplateParamSet = [code,] # 模板参数: 若无模板参数，则设置为空
        req.PhoneNumberSet = [phone,]
        resp = client.SendSms(req) #发送短信
        # 返回内容判断
        if resp.SendStatusSet[0].Code == "Ok":
            return True
        # 输出json格式的字符串回包
        print(resp.to_json_string(indent=2))
    except TencentCloudSDKException as err:
        print(err)